
import time,os
import xlwt

def get_memory(pid): # 输出窗口命令 返回获取值
    cmd='adb shell dumpsys meminfo ' + pid
    contents = os.popen(cmd)
    for line in contents.readlines():
        if 'TOTAL   ' in line:
            return line.split(' ')[11].strip()

    #         memorys = line.split(' ')
    #         print("memorys：", memorys)
    #         memory_1 = line.split('TOTAL   ')[1]
    #         print("memory_1：", memory_1)
    #         memory = memory_1.split("   ")[0]
    #         # memory = memory_1.split('TOTAL SWAP PSS:  ')[0]
    #         print("memory：", memory)
    #         break
    # try:
    #     return memory.strip()
    # except UnboundLocalError:
    #     return 0

def get_cpu(package): # 输入命令获取使用cpu的值
    cmd='adb shell top -n 1'
    content = os.popen(cmd)
    for line in content.readlines():
        if package in line:
            infos = line.split(" ")
            # print("line[]：", line.split(" "))
            return infos[infos.index("S") + 1].strip()

    #         cpu1=line.split('%')[0]
    #         print("cpu1：", cpu1)
    #         cpu2=cpu1.split('20   0')[1]
    #         break
    # return cpu2.strip()

def get_electric_quantity(): # 输入命令获取使用电量的值
    cmd = 'adb shell dumpsys battery'
    contents = os.popen(cmd)
    for line in contents.readlines():
        if 'level: ' in line:
            return line.split(' ')[3].strip()

    #         print("line[]：", line.split(' '))
    #         print("electrics：", line.split('level: '))
    #         electric = line.split('level: ')[1]
    #         print("electric：", electric)
    #         break
    # try:
    #     return electric.strip()
    # except UnboundLocalError:
    #     return 0

def get_flow(): # 输入命令获取使用流量的值
    pass

if __name__ == '__main__':

    # adb shell dumpsys window | findstr mCurrentFocus  获取包名、活动名
    # adb shell am start -n package/package-activity  启动app

    # adb shell cat /proc/meminfo 获取手机总内存

    # adb shell dumpsys gfxinfo  获取帧率

    #  564,408K   379,140K

    file = xlwt.Workbook()  # 创建表格

    table_memory = file.add_sheet('memory', cell_overwrite_ok=True)
    table_memory.write(0, 0, '时间')
    table_memory.write(0, 1, '占用内存(KB)')

    table_cpu = file.add_sheet('cpu', cell_overwrite_ok=True)
    table_cpu.write(0, 0, '时间')
    table_cpu.write(0, 1, 'CPU占用(%)')

    table_electric = file.add_sheet('electric', cell_overwrite_ok=True)
    table_electric.write(0, 0, '时间')
    table_electric.write(0, 1, '电量(%)')

    package = 'tv.danmaku.bili'

    contents = os.popen("adb shell pidof " + package)
    pid = contents.readlines()[0]

    row = 1

    while True: # 持续测试
        # tv.danmaku.bili.MainActivityV2   活动名

        now = time.strftime('%H:%M:%S', time.localtime())

        memory = get_memory(pid)
        table_memory.write(row, 0, now)
        table_memory.write(row, 1, int(memory))

        cpu = get_cpu(package)
        # print("cpu：", cpu, "|", type(cpu))
        # print("[]：", cpu.split(' '))
        table_cpu.write(row, 0, now)
        table_cpu.write(row, 1, float(cpu))

        electric = get_electric_quantity()
        table_electric.write(row, 0, now)
        table_electric.write(row, 1, int(electric))

        try:
            file.save('app内存cpu测试.xls')
        except PermissionError:
            pass

        print(time.strftime('%H:%M:%S', time.localtime()), ' | ', memory, " | ", cpu, " | ", electric)

        row += 1
        time.sleep(1)































